<?php

namespace App\Admin\Controllers\School;


use App\Models\School\Exam;
use App\Models\School\ExamDetail;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use Illuminate\Support\Facades\DB;
use Symfony\Component\HttpFoundation\Request;

class ExamController extends Controller
{
    use ModelForm;

    public $header = '成绩列表';
    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Exam::class, function (Grid $grid) {

            $grid->model()->orderBy('exam_id','desc');
            $grid->exam_id('编号')->sortable();
            $grid->user()->telephone('手机号');
            $grid->user()->username('发布用户');
            $grid->classes()->name('班级');

            $grid->column('考试科目')->display(function (){
                $subject = json_decode($this->subject_information, true);

                $subject_name = array_column($subject, 'subject_name');
                array_shift($subject_name);
                return implode(',', $subject_name);
            });

            $grid->create_at('发布时间');

            $grid->actions(function (Grid\Displayers\Actions $actions){
                $actions->disableEdit();
            });

            $grid->filter(function (Grid\Filter $filter){
                $filter->disableIdFilter();
                $filter->equal('class_id', '班级id');
                $filter->like('class_name', '班级');
                $filter->between('create_at', '发布时间')->date();
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Exam::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header($this->header)
            ->description('Detail')
            ->row($this->detail($id));
    }

    public function detail($id)
    {
        $exam = Exam::where('exam_id',$id)->first()->toArray();
        if (!$exam){
            abort(404);
        }

        $subject = json_decode($exam['subject_information'], true);
        array_shift($subject);

        $exam_detail = ExamDetail::where('exam_id', $exam['exam_id'])->get()->toArray();

        $data = [];
        foreach ($exam_detail as $key=>$val){
            $data[$val['student_id']]['student_name'] = $val['stu_name'];
            $data[$val['student_id']]['detail'][$val['subject_code']] = ['subjects'=>$val['subjects'],'score'=>$val['score']];
        }

        foreach ($data as $key=>$val){
            $total_score = array_sum(array_column($val['detail'],'score'));
            $data[$key]['total_score'] = $total_score;
            $data[$key]['avg_score'] = round($total_score / count($val['detail']),2);
        }
        return view('admin.school.exam',compact('subject','data'));


    }

}
